/**
 * @Title: XunjianRecordServiceImpl.java
 * @Package me.springboot.mybatis.service.impl
 * @Description: TODO
 * @author zengh
 * @date 2018/04/23
 * @version V1.0
 */
package me.springboot.mybatis.service.impl;

import me.springboot.mybatis.common.utils.DateUtils;
import me.springboot.mybatis.common.utils.StringUtil;
import me.springboot.mybatis.core.AbstractService;
import me.springboot.mybatis.core.RetCode;
import me.springboot.mybatis.dao.DeviceMapper;
import me.springboot.mybatis.dao.UserMapper;
import me.springboot.mybatis.dao.XunjianRecordMapper;
import me.springboot.mybatis.exception.ResultException;
import me.springboot.mybatis.model.Device;
import me.springboot.mybatis.model.User;
import me.springboot.mybatis.model.XunjianRecord;
import me.springboot.mybatis.service.XunjianRecordService;
import me.springboot.mybatis.web.req.XunJianReqRecord;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * @author zengh
 * @ClassName: XunjianRecordService
 * @Description: TODO
 * @date 2018/04/23
 */
@Service
@Transactional
public class XunjianRecordServiceImpl extends AbstractService<XunjianRecord> implements XunjianRecordService {

    private static final Logger logger = Logger.getLogger(XunjianRecordServiceImpl.class);

    @Resource
    private XunjianRecordMapper xunjianRecordMapper;

    @Resource
    private DeviceMapper deviceMapper;

    @Resource
    private UserMapper userMapper;

    @Override
    public List<Map<String, Object>> selectByReq(XunJianReqRecord xunJianReqRecord) {
        List<Map<String, Object>> list = xunjianRecordMapper.selectByReq(xunJianReqRecord);
        List<Map<String, Object>> resultList = new ArrayList<>();
        if (StringUtil.isNotBlank(xunJianReqRecord.getEndTime())) {
            for (Map<String, Object> map : list) {
                if (map.containsKey("xunjian_time") && null != map.get("xunjian_time")) {
                    Timestamp timestamp = (Timestamp) map.get("xunjian_time");
                    long date = timestamp.getTime();
                    if (StringUtil.isNotBlank(date)) {
                        if (date <= Long.valueOf(xunJianReqRecord.getEndTime())) {
                            resultList.add(map);
                        }
                    }
                }
            }
            return resultList;
        } else {
            return list;
        }
    }

    @Override
    public void dispatch(String xunjianTime, String devicenums, String userId) throws ResultException {
        if (StringUtils.isEmpty(devicenums) || StringUtil.isEmpty(userId) || StringUtil.isEmpty(xunjianTime)) {
            throw new ResultException(RetCode.BUS_FALL.getCode(), "缺失参数");
        }
        String deviceNum[] = devicenums.split(",");
        User queryUser = new User();
        queryUser.setuId(Integer.valueOf(userId));
        User user = userMapper.selectByModel(queryUser);
        int result = 0;
        for (String s : deviceNum) {
            Device device = new Device();
            device.setEquipmentNum(s);
            List<Device> devices = deviceMapper.selectModel(device);
            logger.info("=================================" + devices.size());
            if (null != devices && devices.size() > 0) {
                logger.info("=================================" + devices.get(0).toString());
                device = devices.get(0);
                XunjianRecord xunjianRecord = new XunjianRecord();
                xunjianRecord.setAddress(device.getAddress());
                xunjianRecord.setDevicenum(s);
                xunjianRecord.setDevicetype(device.getDevicetype());
                xunjianRecord.setNum(device.getNum());
                xunjianRecord.setStatus(0+"");
                xunjianRecord.setXunjianBefore(device.getDeviceStatus().toString());
                xunjianRecord.setXunjianName(user.getName());
                xunjianRecord.setType(1);
                xunjianRecord.setXunjianUserid(user.getuId());
                xunjianRecord.setXunjianTime(DateUtils.stringToDate(xunjianTime, DateUtils.DATE_TIME_FORMAT));

                result = xunjianRecordMapper.insert(xunjianRecord);
                logger.info(" dispatch [userid:" + userId + ",devicenum:" + s + ",result:" + result + "] ");

                device.setXunjianUserId(user.getuId());
                device.setXunjianName(user.getName());
                deviceMapper.updateByPrimaryKey(device);
            }
        }
    }

    /**
     * 转单
     *
     * @param xunjianRecordId
     * @param userId
     */
    @Override
    public void slip(String xunjianRecordId, String userId) throws ResultException {
        XunjianRecord xunjianRecord = xunjianRecordMapper.selectByPrimaryKey(Integer.valueOf(xunjianRecordId));
        if (null != xunjianRecord) {
            User user = userMapper.selectByPrimaryKey(Integer.valueOf(userId));
            xunjianRecord.setXunjianUserid(user.getuId());
            xunjianRecord.setXunjianName(user.getName());
            xunjianRecordMapper.updateByPrimaryKey(xunjianRecord);

            Device queryDevice = new Device();
            queryDevice.setEquipmentNum(xunjianRecord.getDevicenum());
            List<Device> devices = deviceMapper.selectModel(queryDevice);
            if(null != devices && devices.size() > 0) {
                Device device = new Device();
                device.setXunjianUserId(user.getuId());
                device.setXunjianName(user.getName());
                deviceMapper.updateByPrimaryKey(device);
            }
        } else {
            throw new ResultException(RetCode.BUS_FALL.getCode(), "巡检记录不存在");
        }
    }

    @Override
    public List<Map<String, Object>> xunjianAddress(Integer userid) {
        Map<String, Object> param = new HashMap<>();
        param.put("userid", userid);
        return xunjianRecordMapper.xunjianAddress(param);
    }
}